﻿Imports System

Public Class frmEditWithSetDefault
   Private _value As ValueType
   Private _type As Type
   Private _defaultValue As ValueType
   Private oldvalue As ValueType

   Public Delegate Sub CloseMe(ByVal sender As Object, ByVal e As EventArgs)
   Private _closeEdit As CloseMe
   Public nib As Object

   Public Sub New()

      ' This call is required by the Windows Form Designer.
      InitializeComponent()

      ' Add any initialization after the InitializeComponent() call.
      TopLevel = False

      nib = System.Activator.CreateInstance(Type.GetType("TnTinMn.NIB.NumericInputBox", True, True))
      Dim nibType As Type = Type.GetType("TnTinMn.NIB.NumericInputBox", True, True)

      Dim mi As Reflection.MethodInfo = nibType.GetMethod("SuppressFormattingUpdates")
      mi.Invoke(nib, New Object() {})

      Reflectionhelpers.SetPropertyOnObject("AdvanceTabStopOnEnter", nib, True)
      Reflectionhelpers.SetPropertyOnObject("TextAlign", nib, System.Windows.Forms.HorizontalAlignment.Right)
      Reflectionhelpers.SetPropertyOnObject("BlankZero", Reflectionhelpers.GetPropPublic("DisplayedFormatProperties", nib), True)

      Dim cultpref As Type = Type.GetType("TnTinMn.NIB.DisplayedFormatProperties+NIBCulturePreference", False, False)

      SetPropertyOnObject("FormattingCulture", _
                           Reflectionhelpers.GetPropPublic("DisplayedFormatProperties", nib), _
                           [Enum].Parse(cultpref, "Invariant"))

      Dim anchors As Type = Type.GetType("TnTinMn.NIB.InfoBox+AnchorPoint", False, False)

      Reflectionhelpers.SetPropertyOnObject("DisplayedMessageBoxAnchorPoint", _
                                           GetPropPublic("ErrorMessageProperties", nib), _
                                           [Enum].Parse(Type.GetType("TnTinMn.NIB.InfoBox+AnchorPoint", False, False), "BottomRight"))

      Reflectionhelpers.SetPropertyOnObject("InputBoxAnchorPoint", _
                                             GetPropPublic("ErrorMessageProperties", nib), _
                                             [Enum].Parse(Type.GetType("TnTinMn.NIB.InfoBox+AnchorPoint", False, False), "TopLeft"))

      mi = nibType.GetMethod("ResumeFormattingUpdates")
      mi.Invoke(nib, New Object() {})

      Dim c As System.Windows.Forms.Control = CType(nib, System.Windows.Forms.Control)
      c.Size = New System.Drawing.Size(156, 20)
      c.Dock = Windows.Forms.DockStyle.Top
      c.TabIndex = 0
      Me.Panel1.Controls.Add(c)

   End Sub

   Public WriteOnly Property CloseRoutine() As CloseMe
      Set(ByVal value As CloseMe)
         _closeEdit = value
      End Set
   End Property

   Public WriteOnly Property SystemTypeCode() As System.TypeCode
      Set(ByVal value As TypeCode)


         Reflectionhelpers.SetPropertyOnObject("SystemTypeCode", nib, value)
         Dim nibsysType As Type = CType(Reflectionhelpers.GetPropNonPublic("SystemType", nib), Type)

         Dim o As Object = CType(Convert.ChangeType(0, nibsysType), Object)

         Dim max As Object = Reflectionhelpers.GetStaticMethod(nib, "GetMax").Invoke(nib, New Object() {nibsysType})
         Reflectionhelpers.SetPropertyOnObject("MaxValue", Reflectionhelpers.GetPropPublic("RangeChecking", nib), max)

         Dim min As Object = Reflectionhelpers.GetStaticMethod(nib, "GetMin").Invoke(nib, New Object() {nibsysType})
         Reflectionhelpers.SetPropertyOnObject("MinValue", Reflectionhelpers.GetPropPublic("RangeChecking", nib), min)
         Select Case _DefaultSetting
            Case DefaultTypeSetting.Max
               _defaultValue = CType(max, ValueType)
            Case DefaultTypeSetting.Min
               _defaultValue = CType(min, ValueType)
         End Select
      End Set
   End Property

   'Public WriteOnly Property systemType() As Type
   '   Set(ByVal value As Type)
   '      _type = value
   '      Dim o As Object = CType(Convert.ChangeType(0, value), Object)

   '   Reflectionhelpers.SetPropertyOnObject("SystemType", nib, value)
   '   Dim max As Object = Reflectionhelpers.GetStaticMethod(nib, "GetMax").Invoke(nib, New Object() {value})
   '   Reflectionhelpers.SetPropertyOnObject("MaxValue", Reflectionhelpers.GetPropPublic("RangeChecking", nib), max)

   '   Dim min As Object = Reflectionhelpers.GetStaticMethod(nib, "GetMin").Invoke(nib, New Object() {value})
   '   Reflectionhelpers.SetPropertyOnObject("MinValue", Reflectionhelpers.GetPropPublic("RangeChecking", nib), min)
   '   Select Case _DefaultSetting
   '      Case DefaultTypeSetting.Max
   '         _defaultValue = CType(max, ValueType)
   '      Case DefaultTypeSetting.Min
   '         _defaultValue = CType(min, ValueType)
   '   End Select
   '   End Set
   'End Property

   Public Property Value() As ValueType
      Get
         Return CType(GetPropPublic("Value", nib), ValueType)
      End Get
      Set(ByVal value_ As ValueType)
         Reflectionhelpers.SetPropertyOnObject("Value", nib, value_)
         oldvalue = value_
      End Set
   End Property

   Public Enum DefaultTypeSetting
      Min
      Max
   End Enum

   Private _DefaultSetting As DefaultTypeSetting = DefaultTypeSetting.Min
   Public Property DefaultSetting() As DefaultTypeSetting
      Get
         Return _DefaultSetting
      End Get
      Set(ByVal value As DefaultTypeSetting)
         _DefaultSetting = value
      End Set
   End Property 'DefaultType

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Reflectionhelpers.SetPropertyOnObject("Value", nib, _defaultValue)
   End Sub

   Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
      _closeEdit.Invoke(Nothing, Nothing)
   End Sub

   Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
      Reflectionhelpers.SetPropertyOnObject("Value", nib, oldvalue)
      _closeEdit.Invoke(Nothing, Nothing)
   End Sub

End Class

